<?php
/**
 * Created by PhpStorm.
 * User: lixy
 * Date: 2016/1/19
 * Time: 13:16
 */

namespace app\controllers;

use Yii;
use app\lib\WXBaseController;
use app\lib\WebService;;
use app\models\HomeDao;
use app\models\FxDao;
use app\lib\WeixinUtils;
use app\lib\Fx_Image;
use app\models\weixin\T_WXMALL_USER;
class WxFxController extends WXBaseController
{




    //我的分销   my-fx
    public function actionMyFx(){


        static::__validUser();

        $user_id = static::getSessionUserid();
        $user_info=FxDao::get_CurrentIndex($user_id);
        $user_info=$user_info[0];
        $x_data=[];
        $y_data=[];
        $reList=FxDao::get_T_WX_USER_CASHINCOME($user_id,8);
        foreach ($reList as $item) {
            $x_data[]= date("m-d",strtotime($item['CREATE_DATE']));
            $y_data[]=$item['CASH'];
        }


        return $this->render("my-fx", ['user_info'=>$user_info,'x_data' => array_reverse($x_data),'y_data'=>array_reverse($y_data)]);
    }

    //业绩列表
    public function actionMyResultList(){
        $user_id = static::getSessionUserid();
        $reList=FxDao::get_MyResultList(0,1,$user_id);
        if(count($reList)==0){
            static::dump_info('暂无记录');
        }
        return $this->render("my-result-list", []);
    }
    //获取我的业绩列表 get-my-result-list-handle
    public function actionGetMyResultListHandle()
    {
        $user_id = static::getSessionUserid();
        $msg = "";
        $flag =1;

        $start=static::getRequestPost('start');
        $pagesize=static::getRequestPost('pagesize');
        $reList=FxDao::get_MyResultList($start,$pagesize,$user_id);

        foreach($reList as &$re){
            $re['CREATE_DATETIME_SHOW']=date("Y-m-d",strtotime($re['CREATE_DATETIME']));
        }
        echo json_encode(['msg' => $msg, 'flag' => $flag, 'reList' => $reList]);
    }
    //现金变化列表
    public function actionMyBalanceList(){
        $user_id = static::getSessionUserid();
        $reList=FxDao::get_MyBalanceList(0,1,$user_id);
        if(count($reList)==0){
            static::dump_info('暂无记录');
        }
        return $this->render("my-balance-list", []);
    }
    //获取我的现金变化列表 get-my-balance-list-handle
    public function actionGetMyBalanceListHandle()
    {
        $user_id = static::getSessionUserid();
        $msg = "";
        $flag =1;

        $start=static::getRequestPost('start');
        $pagesize=static::getRequestPost('pagesize');
        $reList=FxDao::get_MyBalanceList($start,$pagesize,$user_id);

        foreach($reList as &$re){
            $re['CREATE_DATETIME_SHOW']=date("Y-m-d",strtotime($re['CREATE_DATETIME']));
        }
        echo json_encode(['msg' => $msg, 'flag' => $flag, 'reList' => $reList]);
    }
    //分销收入进度 result-schedule
    public function actionResultSchedule($rid){

        $user_id = static::getSessionUserid();
        $re=FxDao::get_user_buy_result_record($rid,$user_id);
        if(count($re)==0){
            echo "not found record";
            exit;
        }
        $re=$re[0];
        return $this->render("result-schedule", ['re'=>$re]);
    }
    //提现进度 get-cash-schedule
    public function actionGetCashSchedule($rid){

        $user_id = static::getSessionUserid();
        $re=FxDao::get_user_buy_result_record($rid,$user_id);
        if(count($re)==0){
            static::dump_info('没有提现记录');
        }
        $re=$re[0];

       $apply= FxDao::get_fx_get_cash_apply($rid,$user_id);
        if(count($apply)==0){
            static::dump_info('没有提现记录');
        }
        $apply=$apply[0];

        return $this->render("get-cash-schedule", ['re'=>$re,'apply'=>$apply]);
    }
    //我的团队  my-team
    public function actionMyTeam(){
        $user_id = static::getSessionUserid();
        $teamCount=FxDao::get_TeamListCount($user_id);
        return $this->render("my-team", ['teamCount'=>$teamCount]);
    }
    //获取我的下线 get-team-list-handle
    public function actionGetTeamListHandle()
    {
        $user_id = static::getSessionUserid();
        $msg = "";
        $flag =1;
        $start=static::getRequestPost('start');
        $pagesize=static::getRequestPost('pagesize');

        $cUserList=FxDao::get_TeamList($start,$pagesize,$user_id);

        foreach($cUserList as &$cUser){
            $cUser['CREATE_DATETIME_SHOW']=date("Y-m-d",strtotime($cUser['CREATE_DATETIME']));
        }
        echo json_encode(['msg' => $msg, 'flag' => $flag, 'cUserList' => $cUserList]);
    }
    //提现   get-cash
    public function actionGetCash(){
        $user_id = static::getSessionUserid();
        $re=FxDao::get_CurrentCash($user_id);
        if($re===false){
            echo "not found data";
            exit;
        }
        return $this->render("get-cash", ['cash'=>$re]);
    }
    //提现申请 get-cash-apply-handle
    public function actionGetCashApplyHandle()
    {
        $user_id = static::getSessionUserid();
        $msg = "";
        $flag =0;
        $cash=static::getRequestPost('cash');

        $re=FxDao::P_WXMALL_CASHAPPLY($user_id,$cash);
        if($re[0]=='1'){
            $flag =1;
        }else{
            $msg = $re[0];
        }

        echo json_encode(['msg' => $msg, 'flag' => $flag, 'cash_record_Id' => $re[1],]);
    }
    //分销界面   my-share
    public function actionMyShare($share_id='',$p_userid='')
    {
        if($share_id==''){
            static::__validUser();

            $user_id = static::getSessionUserid();
            $share_id=FxDao::get_INVITE_CODE($user_id);
           return $this->redirect(["wx-fx/my-share", 'share_id' =>$share_id,'p_userid'=>$user_id]);

        }
       $user=T_WXMALL_USER::getUserByUserid($p_userid);
        $user=$user[0];
        return $this->render("my-share", ['share_id'=>$share_id,'user'=>$user]);
    }

    //分享图   my-share-pic
    public function actionMySharePic($share_id){
       $re=FxDao::get_T_WX_FX_USER_INVITE_CODE($share_id);
        if(count($re)==0){
            echo "error share id!";
            exit;
        }else{
            $NICK_NAME=$re[0]['NICK_NAME'];
            $HEADER_URL=$re[0]['HEADER_URL'];
            $QR_TICKET=$re[0]['QR_TICKET'];
            $days=intval($re[0]['DAYS']);
            if($days>20){
                //更新ticket
                $QR_TICKET= FxDao::update_INVITE_CODE_QR($share_id);
            }
        }

           $imgs = array(
               'header' => $HEADER_URL,
               'qr' => "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket={$QR_TICKET}",
               'bg' => './js/fx/fx_share_bg.jpg',
               'text' => $NICK_NAME,
               'font' => './js/fx/msyh.ttc',
           );
         ob_clean();
        header("Content-type: image/jpeg");

        imagejpeg( Fx_Image::mergerImg($imgs));
        exit;
    }
    //分享排行   金额
    public function actionFxRank1(){
//        return $this->render("fx-rank1");

        static::__validUser();

        $user_id = static::getSessionUserid();
        $userinfo=T_WXMALL_USER::getUserByUserid($user_id);
        $userinfo=$userinfo[0];
        $my_val=$userinfo['PAY_RESULT']==null ?0 : $userinfo['PAY_RESULT'];
        $my_top=FxDao::get_rank1_my($my_val);

        return $this->render("fx-rank1", [
            'userinfo'=>$userinfo,
            'my_val'=>$my_val,
            'my_top'=>$my_top,
        ]);
    }
    // fx-rank1-handle
    public function actionFxRank1Handle(){
        $msg = "";
        $flag =0;
        $start=static::getRequestPost('start');
        $pagesize=static::getRequestPost('pagesize');
        $reList=FxDao::get_rank1_list($start,$pagesize);
        $flag=1;
        echo json_encode(['msg' => $msg, 'flag' => $flag, 'reList' => $reList]);
    }
    //分享排行   人数
    public function actionFxRank2(){
        //fx_user_count
//        return $this->render("fx-rank2");
        static::__validUser();

        $user_id = static::getSessionUserid();
        $userinfo=T_WXMALL_USER::getUserByUserid($user_id);
        $userinfo=$userinfo[0];
        $my_val=$userinfo['FX_USER_COUNT']==null ?0 : $userinfo['FX_USER_COUNT'];
        $my_top=FxDao::get_rank2_my($my_val);

        return $this->render("fx-rank2", [
            'userinfo'=>$userinfo,
            'my_val'=>$my_val,
            'my_top'=>$my_top,
        ]);
    }
    // fx-rank2-handle
    public function actionFxRank2Handle(){
        $msg = "";
        $flag =0;
        $start=static::getRequestPost('start');
        $pagesize=static::getRequestPost('pagesize');
        $reList=FxDao::get_rank2_list($start,$pagesize);
        $flag=1;
        echo json_encode(['msg' => $msg, 'flag' => $flag, 'reList' => $reList]);
    }
}